package com.biannuo.szk.common.vo;


import com.biannuo.szk.common.enums.ResponseCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回数据格式
 *
 * @author szk
 */
@Data
public class ResponseVO<T> implements Serializable {

    /**
     * 状态码
     */
    private String code;
    /**
     * 信息
     */
    private String msg;
    /**
     * 数据
     */
    public T data;

    public ResponseVO() {
    }

    public ResponseVO(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseVO(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseVO(ResponseCodeEnum resultEnums) {
        this.code = resultEnums.getCode();
        this.msg = resultEnums.getMsg();
    }

    public ResponseVO(ResponseCodeEnum resultEnums, T data) {
        this.code = resultEnums.getCode();
        this.msg = resultEnums.getMsg();
        this.data = data;
    }

    /**
     * 成功，返回数据
     *
     * @param data
     * @return
     */
    public static ResponseVO success(Object data) {
        ResponseVO resp = new ResponseVO();
        resp.data = data;
        return resp;
    }

    /**
     * 失败，返回数据
     *
     * @param data
     * @return
     */
    public static ResponseVO fail(Object data) {
        ResponseVO resp = new ResponseVO();
        resp.data = data;
        return resp;
    }

    /**
     * 成功
     *
     * @return
     */
    public static ResponseVO ok() {
        return new ResponseVO(ResponseCodeEnum.CODE_200.getCode(), ResponseCodeEnum.CODE_200.getMsg());
    }

    /**
     * 成功，数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseVO ok(T data) {
        return new ResponseVO<T>(ResponseCodeEnum.CODE_200.getCode(), ResponseCodeEnum.CODE_200.getMsg(), data);
    }

    /**
     * 失败，信息
     *
     * @param msg
     * @return
     */
    public static ResponseVO fail(String msg) {
        return new ResponseVO(ResponseCodeEnum.CODE_500.getCode(), msg);
    }

    /**
     * 失败，返回码和信息
     *
     * @param code
     * @param msg
     * @return
     */
    public static ResponseVO fail(String code, String msg) {
        return new ResponseVO(code, msg);
    }


    @Override
    public String toString() {
        return "ResponseVO{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

}